package com.polaris.live.popup

import android.content.Context
import android.view.View
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.adapter.party.RoomManagementAdapter
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.LiveConst.LivePage
import com.polaris.live.databinding.PopupVideoMoreBinding
import com.polaris.live.dialog.RoomSkinDialog
import com.polaris.live.dialog_fragment.TaskDialogFragment
import com.polaris.live.popup.base.BaseLiveBottomPopupView
import com.polaris.live.ui.party.bean.ManagementBean
import com.polaris.live.utils.TaskRedProxy
import com.polaris.live.utils.fragment.FragmentUtils

/**
 * VideoMorePopup       通话页更多弹窗
 *
 * @author Created by 半仙 on 2023/9/9/09 15:41
 */
class VideoMorePopup(
    context: Context,
    @LivePage
    private val pageType: Int,
    private val isBlock: Boolean = false,
    private val mIsOpenGift: Boolean = true,
    private val mIsOpenSound: Boolean = true,
    private val viewModel: BaseViewModel,
    private val onOpenGiftAnimation: ((state: Boolean) -> Unit)? = null,
    private val onOpenGiftSound: ((state: Boolean) -> Unit)? = null,
    private val onInvitationFriend: (() -> Unit)? = null, //邀请好友
    private val onSelectFunction: BottomPopupView.(select: Int) -> Unit,
) : BaseLiveBottomPopupView<PopupVideoMoreBinding>(context, { PopupVideoMoreBinding.bind(it) }) {

    override fun getImplLayoutId(): Int {
        return R.layout.popup_video_more
    }

    private val mAdapter by lazy { RoomManagementAdapter() }
    private val mLayoutManager by lazy {
        GridLayoutManager(
            context,
            4,
            GridLayoutManager.VERTICAL,
            false
        )
    }

    override fun onCreate() {
        super.onCreate()
        mAdapter.setOnItemClickListener { _, _, position ->
            val data = mAdapter.getItem(position)
            onItemClick(data)
        }
        mBinding.recycler.apply {
            adapter = mAdapter
            layoutManager = mLayoutManager
        }

        addData()
    }

    private fun addData() {
        val isVideo =
            pageType == LiveConst.Page.LIVE_MATCH || pageType == LiveConst.Page.LIVE_ANCHOR || pageType == LiveConst.Page.LIVE_SPECTATOR
        val show =
            pageType == LiveConst.Page.LIVE_MATCH || pageType == LiveConst.Page.LIVE_ANCHOR || pageType == LiveConst.Page.LIVE_SPECTATOR
        if (show) {
            //礼物动效
            mAdapter.addData(
                ManagementBean(
                    gift_animation,
                    StringUtils.getString(com.polaris.live.translation.R.string.gift_animation),
                    null,
                    false,
                    3,
                    mIsOpenGift,
                    R.drawable.btn_room_gift,
                    null,
                ) { isSelected, position ->
                    val data = mAdapter.getItem(position)
                    val state = data.state
                    data.state = !state
                    onOpenGiftAnimation?.invoke(!state)

                    val indexOfFirst =
                        mAdapter.data.indexOfFirst { it.operateId == audio_animation }
                    if (indexOfFirst != -1) {
                        val audioData = mAdapter.getItem(indexOfFirst)
                        audioData.state = data.state
                        mAdapter.notifyItemChanged(indexOfFirst)
                    }
                    mAdapter.notifyItemChanged(position)
                }
            )

            //礼物音效
            mAdapter.addData(
                ManagementBean(
                    audio_animation,
                    StringUtils.getString(com.polaris.live.translation.R.string.sound_string),
                    null,
                    false,
                    3,
                    mIsOpenSound,
                    R.drawable.btn_room_sound,
                    null,
                ) { isSelected, position ->
                    val data = mAdapter.getItem(position)
                    val state = data.state
                    //只有礼物动效开着，音效才能开
                    val indexOfFirst =
                        mAdapter.data.indexOfFirst { it.operateId == gift_animation }
                    if (indexOfFirst == -1) return@ManagementBean
                    val gift = mAdapter.getItem(indexOfFirst)
                    //如果礼物动效开着呢就通知下去
                    if (gift.state) {
                        data.state = !state
                        onOpenGiftSound?.invoke(!state)
                    } else {
                        //如果礼物动效关着呢就不要改变状态
                        data.state = false
                        onOpenGiftSound?.invoke(true)
                    }
                    mAdapter.notifyItemChanged(position)
                }
            )
        }
        if (pageType != LiveConst.Page.LIVE_SPECTATOR) { //不是观众，才有反馈这些
            mAdapter.addData(
                ManagementBean(
                    beauty,
                    StringUtils.getString(com.polaris.live.translation.R.string.beauty_beautify),
                    null,
                    false,
                    1,
                    true,
                    R.drawable.btn_beauty,
                    null,
                )
            )
            mAdapter.addData(
                ManagementBean(
                    turn,
                    StringUtils.getString(com.polaris.live.translation.R.string.switch_camera),
                    null,
                    false,
                    1,
                    true,
                    R.drawable.btn_turn,
                    null,
                )
            )
        }
        if (pageType != LiveConst.Page.CALL) {
            mAdapter.addData(
                ManagementBean(
                    task,
                    StringUtils.getString(com.polaris.live.translation.R.string.daily_tasks),
                    null,
                    TaskRedProxy.isRedAvailable(),
                    1,
                    true,
                    R.drawable.task_room_btn,
                    null,
                )
            )
            if (pageType != LiveConst.Page.LIVE_SPECTATOR) {
                mAdapter.addData(
                    ManagementBean(
                        room_skin,
                        StringUtils.getString(com.polaris.live.translation.R.string.room_skin),
                        null,
                        false,
                        1,
                        true,
                        R.drawable.btn_room_skin,
                        null,
                    )
                )
            }
        }
        if (pageType != LiveConst.Page.CALL && pageType != LiveConst.Page.LIVE_MATCH) {
            //邀请好友
            mAdapter.addData(
                ManagementBean(
                    invitation_friend,
                    StringUtils.getString(com.polaris.live.translation.R.string.invitation_friend),
                    null,
                    false,
                    1,
                    true,
                    R.drawable.btn_invitation_friend,
                    null,
                )
            )
        }
        if (pageType != LiveConst.Page.LIVE_SPECTATOR) { //不是观众，才有摄像头控制这些
            if (isVideo) {
                mAdapter.addData(
                    ManagementBean(
                        feedback,
                        StringUtils.getString(com.polaris.live.translation.R.string.feedback),
                        null,
                        false,
                        1,
                        true,
                        R.drawable.btn_feedback,
                        null,
                    )
                )
                mAdapter.addData(
                    ManagementBean(
                        closeVideo,
                        StringUtils.getString(com.polaris.live.translation.R.string.close),
                        null,
                        false,
                        1,
                        true,
                        R.drawable.btn_close,
                        null,
                    )
                )
            } else {
                mAdapter.addData(
                    ManagementBean(
                        report,
                        StringUtils.getString(com.polaris.live.translation.R.string.report),
                        null,
                        false,
                        1,
                        true,
                        R.drawable.btn_report,
                        null,
                    )
                )


                mAdapter.addData(
                    ManagementBean(
                        blackList,
                        StringUtils.getString(com.polaris.live.translation.R.string.block),
                        null,
                        false,
                        2,
                        !isBlock,
                        R.drawable.btn_blacklist,
                        R.drawable.btn_de_blacklist,
                    )
                )

            }
        }

    }

    private fun onItemClick(data: ManagementBean) {
        when (data.operateId) {
            beauty -> {
                onSelectFunction.invoke(this@VideoMorePopup, beauty)
            }

            turn -> {
                onSelectFunction.invoke(this@VideoMorePopup, turn)
            }

            report -> {
                onSelectFunction.invoke(this@VideoMorePopup, report)
            }

            blackList -> {
                onSelectFunction.invoke(this@VideoMorePopup, blackList)
            }

            feedback -> {
                onSelectFunction.invoke(this@VideoMorePopup, feedback)
            }

            closeVideo -> {
                onSelectFunction.invoke(this@VideoMorePopup, closeVideo)
            }

            invitation_friend -> {
                onSelectFunction.invoke(this@VideoMorePopup, invitation_friend)
            }

            task -> {
                dismiss()
                val fragmentManager = FragmentUtils.getFragmentManager(context) ?: return
                TaskDialogFragment().show(fragmentManager, TaskDialogFragment.TAG)
            }

            room_skin -> {
                RoomSkinDialog.show(context, viewModel)
                dismiss()
            }
        }
    }

    override fun getCardView(): CardView = mBinding.flBg

    override fun getContentView(): View = mBinding.bottom

    companion object {

        const val beauty = 1                 //美颜
        const val turn = 2                   //切换摄像头
        const val report = 3                 //举报
        const val blackList = 4              //拉黑
        const val feedback = 5               //反馈
        const val closeVideo = 6             //关闭直播
        const val task = 7                   //任务
        const val gift_animation = 8         //礼物动效开关
        const val audio_animation = 9        //礼物音效开关
        const val invitation_friend = 10     //邀请好友
        const val room_skin = 11                //房间皮肤
    }
}